/*
 * Created on 2005-3-4
 */
package com.hd123.rumba.request;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * @author caili
 */
public class RequestInformation {
  private Hashtable headParameters;

  public Hashtable getHeaderParameters() {
    return headParameters;
  }
  
  public String getHeaderParametersString() {
    StringBuffer sb = new StringBuffer(headParameters.size()+1);
    sb.append("Header Parameters:<br>");
    Enumeration keys = headParameters.keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      sb.append(key + "=" + headParameters.get(key) + "<br>");
    }
    return sb.toString();
  }

  public RequestInformation(HttpServletRequest httpservletrequest) {
    headParameters = new Hashtable();
    getHeaderInfos(httpservletrequest);
  }

  protected void getHeaderInfos(HttpServletRequest httpservletrequest) {
    Enumeration enumeration = httpservletrequest.getHeaderNames();
    if (enumeration != null) {
      StringBuffer stringbuffer = new StringBuffer();
      stringbuffer.append('_');
      while (enumeration.hasMoreElements()) {
        String s1 = (String) enumeration.nextElement();
        if (s1.startsWith("X-IS-")) {
          String s3 = httpservletrequest.getHeader(s1);
          s1 = s1.substring("X-IS-".length(), s1.length());
          stringbuffer.append(s1.toUpperCase());
          headParameters.put(stringbuffer.toString(), s3);
          stringbuffer.setLength(1);
        }
      }
    }
    String s = (String) headParameters.get("_SERVER_PORT_SECURE");
    String s2 = "80";
    if (s != null)
      if ("0".equals(s)) {
        headParameters.put("_SCHEME", "http");
      } else {
        headParameters.put("_SCHEME", "https");
        s2 = "443";
      }
    String s4 = (String) headParameters.get("_HOST");
    if (s4 != null) {
      int i = s4.indexOf(58);
      if (i != -1) {
        headParameters.put("_HOST", s4.substring(0, i));
        headParameters.put("_SERVER_PORT", s4.substring(i + 1));
      } else {
        headParameters.put("_SERVER_PORT", s2);
      }
    }
    String s5 = httpservletrequest.getHeader("X-IS-SSL_CLIENT_CERT");
    if (s5 != null && !s5.equals("")) {
      StringBuffer stringbuffer1 = new StringBuffer();
      stringbuffer1.append("-----BEGIN CERTIFICATE-----\n");
      s5 = s5.trim();
      int j = s5.length();
      int k;
      for (k = 0; k + 64 <= j; k += 64) {
        stringbuffer1.append(s5.substring(k, k + 64));
        stringbuffer1.append("\n");
      }

      if (k < j) {
        stringbuffer1.append(s5.substring(k));
        stringbuffer1.append("\n");
      }
      stringbuffer1.append("-----END CERTIFICATE-----\n");
      try {
        String s6 = stringbuffer1.toString();
        CertificateFactory certificatefactory = CertificateFactory
            .getInstance("X.509");
        java.security.cert.Certificate certificate = certificatefactory
            .generateCertificate(new ByteArrayInputStream(s6.getBytes(), 0, s6
                .length()));
        headParameters.put("_REQUEST_CLIENT_CERTIFICATE", certificate);
      } catch (CertificateException certificateexception) {
        Logger.getLogger(this.getClass()).warn(certificateexception.getMessage());
      }
    }
    if (!headParameters.containsKey("_SERVER_PORT"))
      headParameters.put("_SERVER_PORT", String.valueOf(httpservletrequest
          .getServerPort()));
    if (!headParameters.containsKey("_SERVER_PROTOCOL"))
      headParameters.put("_SERVER_PROTOCOL", httpservletrequest.getProtocol());
    if (!headParameters.containsKey("_SCHEME"))
      headParameters.put("_SCHEME", httpservletrequest.getScheme());
    if (!headParameters.containsKey("_HOST"))
      headParameters.put("_HOST", httpservletrequest.getServerName());
    if (!headParameters.containsKey("_SCRIPT_NAME"))
      if (httpservletrequest.getServletPath() != null)
        headParameters.put("_SCRIPT_NAME", httpservletrequest.getServletPath());
      else
        headParameters.put("_SCRIPT_NAME", "");
  }

}
